package com.todo.biz.application.frontend.service.impl;

import com.todo.biz.application.convert.BizBaseConvert;
import com.todo.biz.application.entity.bo.CollectRelationBO;
import com.todo.biz.application.entity.bo.CollectSetBO;
import com.todo.biz.application.frontend.service.CollectSetAggService;
import com.todo.biz.application.remote.PlanSetRemoteService;
import com.todo.biz.application.remote.PlatformStrategyRemoteService;
import com.todo.biz.application.remote.RestaurantInfoRemoteService;
import com.todo.biz.application.remote.SightInfoRemoteService;
import com.todo.biz.application.service.CollectRelationService;
import com.todo.biz.application.service.CollectSetService;
import com.todo.biz.common.enums.CollectSetTypeEnum;
import com.todo.biz.iface.frontend.request.CollectSetRelationBaseParam;
import com.todo.biz.iface.frontend.request.QueryCollectSetByCityParam;
import com.todo.biz.iface.frontend.request.QueryCollectSetByIdParam;
import com.todo.biz.iface.frontend.response.*;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.delicacy.service.api.request.QueryRestaurantInfoByIdListRequest;
import com.todo.delicacy.service.api.response.RestaurantInfoResponse;
import com.todo.sight.service.api.response.SightInfoResponse;
import com.todo.strategy.online.service.api.response.PlanSetInfoResponse;
import com.todo.strategy.online.service.api.response.PlatformStrategyBaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (CollectSet)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:45
 */
@Slf4j
@Service("collectSetAggService")
public class CollectSetAggServiceImpl implements CollectSetAggService {

    @Resource
    private CollectSetService collectSetService;
    @Resource
    private CollectRelationService collectRelationService;
    @Resource
    private PlatformStrategyRemoteService platformStrategyRemoteService;
    @Resource
    private SightInfoRemoteService sightInfoRemoteService;
    @Resource
    private RestaurantInfoRemoteService restaurantInfoRemote;
    @Autowired
    private PlanSetRemoteService planSetRemoteService;


    @Override
    public BizResponse<List<CollectSetVO>> queryCollectSetByCity(QueryCollectSetByCityParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        List<CollectSetBO> collectSetBOList = collectSetService.queryCollectSetByCity(param.getCity(), CollectSetTypeEnum.getByType(param.getCollectType()));
        return BizResponse.success(BizBaseConvert.convertToCollectSetVOList(collectSetBOList));
    }

    @Override
    public BizResponse<CollectSetVO> queryCollectSetById(QueryCollectSetByIdParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        CollectSetBO collectSetBO = collectSetService.queryCollectSetById(Long.valueOf(param.getCollectId()));
        return BizResponse.success(BizBaseConvert.convertToCollectSetVO(collectSetBO));
    }

    @Override
    public BizResponse<List<CollectRestaurantSetVO>> queryCollectRestaurantSet(CollectSetRelationBaseParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        List<CollectRelationBO> collectRelationBOList = collectRelationService.queryCollectRelationBOBySetId(param.getSetId());
        if (CollectionUtils.isEmpty(collectRelationBOList)) {
            return BizResponse.success("数据为空，请刷新后重试");
        }
        //TODO 这类数据基本不变，我们额外设置关联缓存 24h 接受调整集合信息的回调进行缓存刷新
        List<Long> relationIdList = collectRelationBOList.stream().map(CollectRelationBO::getRelationId).collect(Collectors.toList());
        QueryRestaurantInfoByIdListRequest request = new QueryRestaurantInfoByIdListRequest();
        request.setRestaurantIdList(relationIdList);
        BizResponse<List<RestaurantInfoResponse>> bizResponse = restaurantInfoRemote.queryRestaurantInfoByIdList(request);
        if (bizResponse == null || !bizResponse.isSuccess() || CollectionUtils.isEmpty(bizResponse.getData())) {
            return BizResponse.success();
        }
        return BizResponse.success(BizBaseConvert.convertToCollectRestaurantSetVOList(bizResponse.getData(), param.getSetId()));
    }

    @Override
    public BizResponse<List<CollectSightSetVO>> queryCollectSightSet(CollectSetRelationBaseParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        List<CollectRelationBO> collectRelationBOList = collectRelationService.queryCollectRelationBOBySetId(param.getSetId());
        if (CollectionUtils.isEmpty(collectRelationBOList)) {
            return BizResponse.success("数据为空，请刷新后重试");
        }
        //TODO 这类数据基本不变，我们额外设置关联缓存 24h 接受调整集合信息的回调进行缓存刷新
        List<Long> relationIdList = collectRelationBOList.stream().map(CollectRelationBO::getRelationId).collect(Collectors.toList());
        BizResponse<List<SightInfoResponse>> bizResponse = sightInfoRemoteService.querySightInfoByPoiIdList(relationIdList);
        if (bizResponse == null || !bizResponse.isSuccess() || CollectionUtils.isEmpty(bizResponse.getData())) {
            return BizResponse.success();
        }
        return BizResponse.success(BizBaseConvert.convertToCollectSightSetVOList(bizResponse.getData(), param.getSetId()));
    }

    @Override
    public BizResponse<List<CollectUserStrategySetVO>> queryCollectUserStrategySet(CollectSetRelationBaseParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        List<CollectRelationBO> collectRelationBOList = collectRelationService.queryCollectRelationBOBySetId(param.getSetId());
        if (CollectionUtils.isEmpty(collectRelationBOList)) {
            return BizResponse.success("数据为空，请刷新后重试");
        }
        //TODO 这类数据基本不变，我们额外设置关联缓存 24h 接受调整集合信息的回调进行缓存刷新
        List<Long> relationIdList = collectRelationBOList.stream().map(CollectRelationBO::getRelationId).collect(Collectors.toList());
        BizResponse<List<PlanSetInfoResponse>> bizResponse = planSetRemoteService.queryPlanSetInfoListByIdList(relationIdList);
        if (bizResponse == null || !bizResponse.isSuccess() || CollectionUtils.isEmpty(bizResponse.getData())){
            return BizResponse.success("数据为空，请刷新后重试");
        }
        return BizResponse.success(BizBaseConvert.convertToCollectUserStrategySetVOList(bizResponse.getData(),param.getSetId()));
    }

    @Override
    public BizResponse<List<CollectPlatformStrategySetVO>> queryCollectPlatformStrategySet(CollectSetRelationBaseParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        List<CollectRelationBO> collectRelationBOList = collectRelationService.queryCollectRelationBOBySetId(param.getSetId());
        if (CollectionUtils.isEmpty(collectRelationBOList)) {
            return BizResponse.success("数据为空，请刷新后重试");
        }
        //TODO 这类数据基本不变，我们额外设置关联缓存 24h 接受调整集合信息的回调进行缓存刷新
        List<Long> relationIdList = collectRelationBOList.stream().map(CollectRelationBO::getRelationId).collect(Collectors.toList());
        BizResponse<List<PlatformStrategyBaseResponse>> bizResponse = platformStrategyRemoteService.queryPlatformStrategyList(relationIdList);
        if (bizResponse == null || !bizResponse.isSuccess() || CollectionUtils.isEmpty(bizResponse.getData())) {
            return BizResponse.success();
        }
        return BizResponse.success(BizBaseConvert.convertToCollectPlatformStrategySetVOList(bizResponse.getData(), param.getSetId()));
    }
}
